#pragma once
#include <iostream>
#include <pthread.h>
#include <string>
#include <cassert>

namespace MYthread
{
    class Thread
    {
    public:
        typedef void*(*func_t)(void*);
    public:
        Thread()
        {
            char buff[100];
            snprintf(buff, sizeof(buff), "线程%d", num_++);
            threadname_ = buff;
        }

        void thread_start(func_t func, void* arg = nullptr)
        {
            func_ = func;
            int n = pthread_create(&tid_, nullptr, func_, arg);
            assert(n == 0);
        }
        
        void thread_join()
        {
            int n = pthread_join(tid_, arg_);
            assert(n == 0);
        }
        
        void thread_cancel()
        {
            int n = pthread_cancel(tid_);
            assert(n == 0);
        }

        std::string thread_name()
        {
            return threadname_;
        }

        ~Thread()
        {
            //
        }
    private:
        static int num_;
        pthread_t tid_;
        std::string threadname_;
        func_t func_;

        void** arg_;
    };
    int Thread::num_ = 1;
}